package com.onyx.service.impl;

import com.onyx.entity.Product;
import com.onyx.interceptor.UserContext;
import com.onyx.mapper.ProductMapper;
import com.onyx.service.ProductService;
import com.onyx.utils.NumUtil;
import com.onyx.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    private static Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);

    private AtomicInteger num = new AtomicInteger(0);

    @Override
    public Product getProductById(Long id) {
        return productMapper.queryProductById(id);
    }

    @Override
    public void createProduct(Product product) {
        logger.info("用户:{} 创建了产品:{}", UserContext.getUser().getId(), product.getName());
        product.setCreateTime(new Date());
        productMapper.save(product);
    }

    @Override
    public void modifyProduct(Product product) {
        logger.info("用户:{} 修改了产品:{}", UserContext.getUser().getId(), product.getName());
        productMapper.modifyProduct(product);
    }

    @Override
    public void deleteProduct(Long id) {
        logger.info("用户:{} 删除了产品:{}", UserContext.getUser().getId(), id);
        productMapper.deleteProduct(id);
    }

    @Override
    public Page<Product> queryAllProduct(Map<String, Object> params) {
        List<Product> list = productMapper.queryAllProduct(params);
        int num = productMapper.queryAllProductNum(params);
        Page<Product> page = new Page<Product>(NumUtil.num2Int(params.get("pageNo")), num, list);
        return page;
    }

    @Override
    public List<Map<String, Object>> queryProducts() {
        return productMapper.queryProducts();
    }

    @Override
    public List<Long> queryProductIds(List<String> productNames) {
        return productMapper.queryProductIds(productNames);
    }

    @Override
    public List<String> searchName(String name) {
        return productMapper.searchName("%" + name + "%");
    }

    @Override
    public String productService(List<String> productNames) {
        List<Map<String, Object>> list = queryProducts();
        List<String> names = new ArrayList<>(list.size());
        for (Map<String, Object> map : list) {
            String value = String.valueOf(map.get("name"));
            names.add(value);
        }
        StringBuilder sb = new StringBuilder();
        for (String name : productNames) {
            if (!names.contains(name)) {
                sb.append(name);
                sb.append(",");
            }
        }
        if (sb.length() > 0) {
            return sb.substring(0, sb.length() - 1);
        } else {
            return "0";
        }
    }

    @Override
    public int findName(String name) {
        return productMapper.findName(name);
    }
}
